package com.asa.jihe.utils;

/**
 * 二维平面上的欧拉几何
 * @author Administrator
 *
 */
public class Euclid {

	
	/**
	 * 关系判断
	 */
	/**
	 * 判断三点是否共线
	 */
	public static boolean inline(Point a,Point b,Point c) {
		double area2 = ToLeftTest.area2(a, b, c);
		return area2==0;
	}
	
	
	
	/**
	 * 判断两条直线是否平行
	 * 思路是把向量平移到原点
	 * 这样就有三点了，判断三点是否共线即可
	 */
	public static boolean pingxing(Line line1,Line line2) {
		
		Point spoint1 = line1.spoint;
		Point epoint1 = line1.epoint;

		Point spoint2 = line2.spoint;
		Point epoint2 = line2.epoint;
		
		Point a = new Point();
		a.x = spoint1.x - epoint1.x;
		a.y = spoint1.y - epoint1.y;

		Point b = new Point();
		b.x = spoint2.x - epoint2.x;
		b.y = spoint2.y - epoint2.y;
		
		Point c = new Point();
		
		c.x = 0;
		c.y = 0;
		
		
		double area2 = ToLeftTest.area2(a, b, c);

		return area2!=0;
	}
	
	
	
	/**
	 * 向量间的点乘
	 */
	public static double diancheng(Point a,Point b) {
		double asa = a.x*b.x + a.y*b.y;
		return asa;
	}
	
	
	
	
	
	/**
	 * 向量间的叉乘
	 */
	public static double chacheng(Point a,Point b) {
		double asa = a.x*b.y + b.x*a.y;
		return asa;
	}
	
	
	
	/**
	 * 两点相减，向量
	 */
	public static Point xiangliang(Point b,Point a) {
		
		Point result = new Point();
		result.x = b.x-a.x;
		result.y = b.y-a.y;
		
		return result;
	}
	
	
	/**
	 * 把数组变成点
	 */
	public static Point toPoint(double[] a) {
		
		Point result = new Point();
		result.x = a[0];
		result.y = a[1];
		
		return result;
	}
	
	
	
	
	/**
	 * 平面上两点的距离
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @return
	 */
	public static double distance(double x1,double y1,double x2,double y2) {
		
		double sqrt = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

		return sqrt;

	}
	
	
	/**
	 * 平面上两点的距离
	 * @param a
	 * @param b
	 * @return
	 */
	public static double distance(Point a, Point b) {
		
		double sqrt = Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
		return sqrt;
	}
	
	
	
	
	
	
	
	
	/**
	 * 三角形的面积计算
	 * @param a
	 * @param b
	 * @param c
	 * @return
	 */
	public static double triangleArea(Point a, Point b,Point c)
	{
	    //已知三角形三个顶点的坐标，求三角形的面积 
	    return Math.abs(a.x * b.y + b.x * c.y + c.x * a.y
	      - b.x * a.y - c.x * b.y - a.x * c.y) / 2;   
	}
	
	/**
	 * 三角形的面积计算
	 * @param t
	 * @return
	 */
	public static double triangleArea(Triangle t)
	{
	    //已知三角形三个顶点的坐标，求三角形的面积 
	    return triangleArea(t.point1, t.point2, t.point3);   
	}
	
	
	
	
	
	public static Circle circumcircleOfTriangle(Triangle t)
	{
	    //三角形的外接圆
	    Circle tmp = new Circle();
	    double a, b, c, c1, c2;
	    double xA, yA, xB, yB, xC, yC;
	    a = distance(t.point1, t.point2);
	    b = distance(t.point2, t.point3);
	    c = distance(t.point3, t.point1);
	    //根据S = a * b * c / R / 4;求半径R 
	    tmp.r = a * b * c / triangleArea(t) / 4;
	    
	    xA = t.point1.x; yA = t.point1.y;
	    xB = t.point2.x; yB = t.point2.y;
	    xC = t.point3.x; yC = t.point3.y;
	    c1 = (xA * xA + yA * yA - xB * xB - yB * yB) / 2;
	    c2 = (xA * xA + yA * yA - xC * xC - yC * yC) / 2;
	    
	    tmp.centre.x = -(c1 * (yA - yC) - c2 * (yA - yB)) / 
	         ((xA - xB) * (yA - yC) - (xA - xC) * (yA - yB)); 
	    tmp.centre.y = -(c1 * (xA - xC) - c2 * (xA - xB)) / 
	         ((yA - yB) * (xA - xC) - (yA - yC) * (xA - xB)); 
	    tmp.centre.x *= -1;
	    tmp.centre.y *= -1;
	         
	    return tmp;     
	}
	
	
	

	
	
	
	
	
	/**
	 * 判断两条线是否是同一条
	 * @param line1
	 * @param line2
	 * @return
	 */
	public static boolean issameline(Line line1,Line line2) {
		
		if (line1.spoint == line2.spoint && line1.epoint == line2.epoint) {
			return true;
		}
		if (line1.spoint == line2.epoint && line1.epoint == line2.spoint) {
			return true;
		}
		
		
		return false;

	}
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
